<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h1 data-lake-id="EsYc7" id="EsYc7"><span data-lake-id="u2107ff37" id="u2107ff37">典型回答</span></h1>
  <p data-lake-id="u74c16c24" id="u74c16c24"><br></p>
  <p data-lake-id="ud56f49ff" id="ud56f49ff"><span data-lake-id="u64903bf0" id="u64903bf0">首先我们需要分析下朋友圈点赞需要有哪些功能，首先记录某个朋友圈的点赞数量，并且支持点赞数数量的查看，支持点赞和取消点赞操作。并且支持查看哪些人点过赞，并且点赞的顺序是可以看得到的。</span></p>
  <p data-lake-id="ufe074fb0" id="ufe074fb0"><span data-lake-id="u24f7a3a4" id="u24f7a3a4">​</span><br></p>
  <p data-lake-id="u42386ee7" id="u42386ee7"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1680416891961-0535f9c7-0f2b-46c6-91a5-27a7759c4246.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_29%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u660f0b35" id="u660f0b35"><span data-lake-id="u5ac6f74e" id="u5ac6f74e">​</span><br></p>
  <p data-lake-id="u402f50f2" id="u402f50f2"><span data-lake-id="ubaa6245c" id="ubaa6245c">那么，基于以上信息，我们可以这样实现：</span></p>
  <p data-lake-id="ua85096bb" id="ua85096bb"><span data-lake-id="uf16e5217" id="uf16e5217">​</span><br></p>
  <p data-lake-id="ud632b8f5" id="ud632b8f5"><span data-lake-id="u4b92c1c3" id="u4b92c1c3">在数据结构上，我们可以采用ZSet来实现，KEY就是这个具体的朋友圈的ID，ZSET的value表示点赞用户的ID，score表示点赞时间的时间戳。这样可以方便地按照时间顺序查询点赞信息，并支持对点赞进行去重，</span></p>
  <p data-lake-id="ucb69a901" id="ucb69a901"><span data-lake-id="u8e0ec649" id="u8e0ec649">​</span><br></p>
  <ol list="ucd4e93fe">
   <li fid="u549e0193" data-lake-id="u7f73ff7e" id="u7f73ff7e"><span data-lake-id="u436a4ef7" id="u436a4ef7">使用字符串存储每篇朋友圈的ID，作为有序集合的KEY。</span></li>
   <li fid="u549e0193" data-lake-id="u1057e31b" id="u1057e31b"><span data-lake-id="uefbbc1d3" id="uefbbc1d3">使用zset存储每篇朋友圈的点赞用户信息，其中value为点赞用户的ID，score为点赞时间的时间戳。</span></li>
   <li fid="u549e0193" data-lake-id="ucc7923f0" id="ucc7923f0"><span data-lake-id="u093c3e32" id="u093c3e32">点赞操作：将用户的ID添加到zset中，score为当前时间戳。如果用户已经点过赞，则更新其点赞时间戳。</span></li>
   <li fid="u549e0193" data-lake-id="u08927493" id="u08927493"><span data-lake-id="ube6d2434" id="ube6d2434">取消点赞操作：将用户的ID从有序集合中删除。</span></li>
   <li fid="u549e0193" data-lake-id="ueac08a80" id="ueac08a80"><span data-lake-id="ue647c798" id="ue647c798">查询点赞信息：使用有序集合的ZREVRANGEBYSCORE命令，按照score（即时间戳）逆序返回zset的value，即为点赞用户的ID。</span></li>
  </ol>
  <p data-lake-id="u8fc157c3" id="u8fc157c3"><span data-lake-id="u6efeaf84" id="u6efeaf84">​</span><br></p>
  <p data-lake-id="u69e975de" id="u69e975de"><span data-lake-id="u3ccbdaa6" id="u3ccbdaa6">以下是代码实现：</span></p>
  <p data-lake-id="ue28f8e80" id="ue28f8e80"><span data-lake-id="ue4bcc6a2" id="ue4bcc6a2">​</span><br></p>
  <pre lang="java"><code>
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.ZParams;
/**
* @author Hollis
**/
public class RedisLikeDemo {
    private static final String LIKE_PREFIX = "like:";
    private static final String USER_PREFIX = "user:";

  	//点赞
    public static void likePost(String postId, String userId, Jedis jedis) {
        String key = LIKE_PREFIX + postId;
        Long now = System.currentTimeMillis();
        jedis.zadd(key, now.doubleValue(), userId);// 将用户ID及当前时间戳加入有序集合
    }

  	//取消点赞
    public static void unlikePost(String postId, String userId, Jedis jedis) {
        String key = LIKE_PREFIX + postId;
        jedis.zrem(key, userId);// 将用户ID从有序集合中移除
    }

  	//查看点赞列表
    public List&lt;String&gt; getLikes(String postId, Jedis jedis) {
        String key = LIKE_PREFIX + postId;
        ZParams zParams = new ZParams().desc();
        return jedis.zrangeByScoreWithScores(key, "+inf", "-inf", 0, -1, zParams)
                .stream()
                .map(tuple -&gt; {
                    String userId = tuple.getElement();
                    return userId;
                }).collect(Collectors.toList());
    }
}

</code></pre>
  <p data-lake-id="u07ec89ad" id="u07ec89ad"><span data-lake-id="ubf4e21ef" id="ubf4e21ef">​</span><br></p>
  <p data-lake-id="u5f7c6eb5" id="u5f7c6eb5"><span data-lake-id="udf12b470" id="udf12b470">在上述代码中，likePost方法用于点赞，unlikePost方法用于取消点赞，getLikes方法用于查询点赞信息。</span></p>
 </body>
</html>